CLASS ycl_moon_http_token DEFINITION
  PUBLIC
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_http_service_extension .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS YCL_MOON_HTTP_TOKEN IMPLEMENTATION.


  METHOD if_http_service_extension~handle_request.
    DATA(req) = request.
    DATA(headers) = req->get_header_fields( ).
    DATA(cookies) = req->get_cookies( ).
    DATA(forms) = req->get_form_fields( ).

    DATA:lv_url TYPE string.
*    lv_url = 'https://services.odata.org/TripPinRESTierService/People'.
    lv_url = 'https://my407000.s4hana.cloud.sap/sap/bc/http/sap/ybcpi_proxy/IntegrationPackages'.
*    lv_url = '/sap/bc/http/sap/ybcpi_proxy/IntegrationPackages'.

    TRY.
        DATA(destination) = cl_http_destination_provider=>create_by_url( lv_url ).
      CATCH cx_http_dest_provider_error INTO DATA(e).
        DATA(errtxt) = e->get_text( ).
        "handle exception
    ENDTRY.

    TRY.
        DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( destination ).
      CATCH cx_web_http_client_error INTO DATA(e2).
        DATA(errtxt2) = e2->get_text( ).

    ENDTRY.
    DATA(request_new) = http_client->get_http_request( ).
    LOOP AT cookies INTO DATA(ls_cookie).
      request_new->set_cookie(
        EXPORTING
          i_name    = ls_cookie-name
*            i_path    = ``
          i_value   = ls_cookie-value
*            i_domain  = ``
*            i_expires = ``
*            i_secure  = 0
*          RECEIVING
*            r_value   =
      ).
    ENDLOOP.

*    CATCH cx_web_message_error.

*    request_new->set_authorization_basic( EXPORTING i_username = 'YB' i_password = 'xDVZjfHPMvSTRjcljniDMEn$HjMBZkDnnPTk9jyv' ).
*    request_new->set_authorization_basic( EXPORTING i_username = 'nanwenfeng@aspnc.com' i_password = 'Aspn2025*' ).



*    request_new->set_header_field(
*          i_name  = 'Authorization'
*          i_value = bearer ).

*    IF i_method = if_web_http_client=>post OR
*       i_method = if_web_http_client=>put OR
*       i_method = if_web_http_client=>delete.
*      TRY.
*          request->set_header_field(
*            i_name  = 'content-type'
*            i_value = i_headers[ name = 'content-type' ]-value ).
*
*          request->set_header_field(
*            i_name  = 'content-length'
*            i_value = i_headers[ name = 'content-length' ]-value ).
*
*          request->set_text( i_body ).
*        CATCH cx_sy_itab_line_not_found.
*      ENDTRY.
*    ENDIF.
    DATA i_method TYPE if_web_http_client=>method.
    i_method = if_web_http_client=>get.
    TRY.
        DATA(meta_response) = http_client->execute( i_method ).
      CATCH cx_web_http_client_error.
        "handle exception
    ENDTRY.
    DATA:headers_new TYPE if_web_http_request=>name_value_pairs,
         r_res       TYPE string,
         bin         TYPE xstring.

    headers_new = meta_response->get_header_fields( ).
    r_res = meta_response->get_text( ).

    data(res_headers) = response->get_header_fields( ).
    data(res_cookies) = response->get_cookies( ).

    response->set_text(
      EXPORTING
        i_text   = `[{ "name": "test111" }, { "name": "test222" }]`
*      i_offset = 0
*      i_length = -1
*    RECEIVING
*      r_value  =
    ).


*  CATCH cx_web_message_error.
  ENDMETHOD.
ENDCLASS.
